// pages/new_address/new_address.js
let feedbackApi = require('../common/showToast.js');
let animation;
let memberId, mId, cookie, token;
const getToken= require('../../utils/get_token.js');

const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  member(that);
  goodShopsn(that);
}
const member = (that) => {
  let params = ['common/receiver/receiver', { "id": mId }]
  getToken.getData(function (res) {
    that.setData({
      s: res.data.receiver,
      address: res.data.address,
      addressId: res.data.addId
    })
  }, ...params)
};
const goodShopsn = (that) => {//省
  let params = ['common/area/selectParentList',{}]
  getToken.getData(function (res) {
    that.setData({
      provinces: res.data.datas
    })
  }, ...params)
}
Page({

  /**
   * 页面的初始数据
   */
	data: {
		s:{},
		address: '',
		addressId:'',
		animationAddressMenu: {},
		addressMenuIsShow: false,
		provinces: [],
		citys: [],
		provinceIndex: 0,
		province: '',
		city: ''
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		var that = this;
		memberId = wx.getStorageSync('memberId');
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    mId = options.id;
		// 初始化动画变量
		var animation = wx.createAnimation({
			duration: 500,
			transformOrigin: "50% 50%",
			timingFunction: 'ease',
		})
		this.animation = animation;
    ckToken(that);
	},
	selectDistrict: function (e) {
		var that = this
		// 如果已经显示，不在执行显示动画
		if (that.data.addressMenuIsShow) {
			return
		}
		// 执行显示动画
		that.startAddressAnimation(true)
	},
	// 执行动画
	startAddressAnimation: function (isShow) {
		var that = this
		if (isShow) {
			// vh是用来表示尺寸的单位，高度全屏是100vh
			that.animation.translateY(0 + 'vh').step()
		} else {
			that.animation.translateY(45 + 'vh').step()
		}
		that.setData({
			animationAddressMenu: that.animation.export(),
			addressMenuIsShow: isShow,
		})
	},
	// 点击地区选择取消按钮
	cityCancel: function (e) {
		this.startAddressAnimation(false)
	},
	// 点击地区选择确定按钮
	citySure: function (e) {
		var that = this
		that.startAddressAnimation(false)
		// 将选择的城市信息显示到输入框
		var address = that.data.province + that.data.city;
		if (address!='')
		{
			that.setData({
				address: address,
			})
		}
	},
	// 点击蒙版时取消组件的显示
	hideCitySelected: function (e) {
		this.startAddressAnimation(false)
	},
	bindchange: function (e) {
		var that = this;
		if (that.data.provinceIndex != e.detail.value[0]) {
			that.data.provinceIndex = e.detail.value[0];
			that.data.province = that.data.provinces[that.data.provinceIndex].name;
      let params = ['common/area/selectChildrenList', {"parentId":that.data.provinces[that.data.provinceIndex].id}]
      getToken.getData(function (res) {
        that.setData({
          citys: res.data.obj,
          addressId: res.data.obj[e.detail.value[1]].id
        })
        that.data.city = that.data.citys[e.detail.value[1]].name;
      }, ...params)
		} else {//改变市区
			that.data.city = that.data.citys[e.detail.value[1]].name;
			that.setData({
				addressId: that.data.citys[e.detail.value[1]].id
			})
		}
	},
	formSubmit: function (e) {//提交
		var that = this;
		var test = /^1(3|4|5|7|8)\d{9}$/;
		var val = e.detail.value;
    // val.name = encodeURIComponent(val.name);
    // val.addressD = encodeURIComponent(val.addressD);
		if (val.name == '') {
			feedbackApi.showToast({ title: '请输入姓名', mask: false })//调用 showToast
			return false;
		}
		if (val.phone == '') {
			feedbackApi.showToast({ title: '请输入手机号码', mask: false })//调用 showToast
			return false;
		}
		if (!val.phone.match(test)) {
			feedbackApi.showToast({ title: '手机号格式不正确', mask: false })//调用 showToast
			return false;
		}
		if (val.address == '') {
			feedbackApi.showToast({ title: '请选择地址', mask: false })//调用 showToast
			return false;
		}
		if (val.addressD == '') {
			feedbackApi.showToast({ title: '请填写详细地址', mask: false })//调用 showToast
			return false;
		}
    let params = [ 'common/receiver/update',{"id": that.data.s.id,"memberId": memberId ,"name": val.name ,"mobile": val.phone ,"zipCode":"1234","areaStore":val.address,"address": val.addressD,"isDefault": val.isBool}]
    getToken.getData(function (res) {
      if (res.data.flag) {
        wx.navigateBack({
          delta: 1
        })
      } else {
        feedbackApi.showToast({ title: res.data.message, mask: false })//调用 showToast
      }
    }, ...params)
	}

})